package com.project.ToDone;

import java.util.ArrayList;


import android.app.ListActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

/**
 * This class determines and defines the activity of the Home screen
 * 
 * @author eliselivingston
 * 
 */
public class HomeActivity extends ListActivity {
	
	public static EventArrayAdapter<Event> adapter;
	public ArrayList<Event> events;
	public HomeActivity selfRef = this;
	public ListView lv;
	public int searchType = 0;
	/**
	 * This method is called when the activity is first created. It sets up the
	 * list view for the ToDone list items.
	 * 
	 * @param Bundle
	 *            savedInstanceState
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		
		MasterControlProgram.setSavedContext(this);
		//Eventually, this will either load the user's events or create an empty array of events to be added to. 
		MasterControlProgram.loadEvents(this);
		MasterControlProgram.saveAlteredEvent(MasterControlProgram.getSavedContext());
		
		
		
		//If the user just was created, saves to databse
		if (MasterControlProgram.getNewUser()) {
			MasterControlProgram.addUserToDatabase(this,MasterControlProgram.getCurrUser());
			//Toast.makeText(getApplicationContext(),
			//		"That was a new user", Toast.LENGTH_SHORT).show();
			MasterControlProgram.setNewUser(false);
		}
		
		//If the event was just created, saves to databse
		if(MasterControlProgram.getNewEvent()!=null){
			
			MasterControlProgram.addEvent(this, MasterControlProgram.getNewEvent());
			MasterControlProgram.setNewEvent(null);
			
			//Toast.makeText(getApplicationContext(),
			//		"That was a new event", Toast.LENGTH_SHORT).show();
			
			MasterControlProgram.loadEvents(this);
		}
		

		//String[] events = MasterControlProgram.getEventsStringArray();   OLD CODE
		events = MasterControlProgram.getToDoListEvents();
		if(events!=null){
			adapter = new EventArrayAdapter<Event>(this, R.layout.list_item, events);
			setListAdapter(adapter);
		}
		
		setContentView(R.layout.home);
		lv = getListView();
		lv.setTextFilterEnabled(true);
		
		//ELISE CODE
		 Button mapButton = (Button) findViewById(R.id.homeMapButton);
		 mapButton.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				Intent i = new Intent(getApplicationContext(), GoogleMapsActivity.class);
				startActivity(i);
				
			}
		});
		 
		 Button logOutButton = (Button) findViewById(R.id.homeLogOutButton);
		 logOutButton.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				Intent i = new Intent(getApplicationContext(), LoginActivity.class);
				startActivity(i);
				
				//Logs out user
				MasterControlProgram.logOut();
				//finish();
				
			}
		});
		//END ELISE CODE
		
		
		
		Button searchButton = (Button) findViewById(R.id.homeSearchButton);
		
		
		searchButton.setOnClickListener(
				new View.OnClickListener() {
					
					
					private String searchParam;
					/**
					 * This method determines what is done upon click on the
					 * Login button. The username and password are both tested
					 * to determine whther an account matches that data.
					 * 
					 * @param View v
					 */
					public void onClick(View v) {
						EditText typeEdit = (EditText)findViewById(R.id.homeEditText);
						searchParam = typeEdit.getText().toString();
						
						if (searchType == 2) 
						{
							if (searchParam.matches("[0|1][0-9]/[0-3][0-9]/[0-9]{2}"))
							{
								events = MasterControlProgram.filterEventsDateAfter(searchParam);
								Toast.makeText(getApplicationContext(), "All of the events after " + searchParam, Toast.LENGTH_LONG).show();
							}
							else
							{
								Toast.makeText(getApplicationContext(), "Try using MM/DD/YY please", Toast.LENGTH_LONG).show();
							}
						}
						else if (searchType == 1) 
						{
							if (searchParam.matches("[0|1][0-9]/[0-3][0-9]/[0-9]{2}"))
							{
								events = MasterControlProgram.filterEventsDateBefore(searchParam);
								Toast.makeText(getApplicationContext(), "All events before " + searchParam, Toast.LENGTH_LONG).show();
							}
							else
							{
								Toast.makeText(getApplicationContext(), "Try using MM/DD/YY please", Toast.LENGTH_LONG).show();
							}
						}
						else
						{
							events = MasterControlProgram.filterEventsType(searchParam);
							Toast.makeText(getApplicationContext(), "All events with the type: " + searchParam, Toast.LENGTH_LONG).show();
						}
						
			    		adapter = new EventArrayAdapter<Event>(selfRef, R.layout.list_item, events);
			    	  	lv.setAdapter(adapter);
						}
					});
		
		//This is all the spinner code (drop down)
		 Spinner spinner = (Spinner) findViewById(R.id.spinner);
		 ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
		            this, R.array.filter_array, android.R.layout.simple_spinner_item);
		 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		 spinner.setAdapter(adapter);
		 spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
		 
		 
		 Button newEventButton = (Button) findViewById(R.id.newEventButton);
		 
		
		lv.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Event clickedEvent = events.get(position);
				String eventname = (String) ((TextView)view).getText();
				clickedEvent.toggleCompletion();
				String comstate;
				
				if (clickedEvent.getCompleted())
				{
					comstate = "COMPLETE!";
					((TextView)view).setTextColor(Color.BLACK);
					
				}
				else
				{
					comstate = "INCOMPLETE!";
					((TextView)view).setTextColor(Color.WHITE);
					
				}
					
				MasterControlProgram.setAlteredEvent(clickedEvent);
				MasterControlProgram.saveAlteredEvent(MasterControlProgram.getSavedContext());
				Toast.makeText(getApplicationContext(),
						eventname + " is now: " + comstate, Toast.LENGTH_SHORT).show();
			}
		});
		

		//Listens for newEvent and links to the NewEventActivity when pressed. 
		newEventButton.setOnClickListener(new View.OnClickListener() {

			/**
			 * This method switches the screen to the register screen if the
			 * user decides to create a new ToDone account.
			 * 
			 * @param View
			 *            v
			 */
			public void onClick(View v) {
				// Switch to register screen
				
				
				Intent i = new Intent(getApplicationContext(),
						NewEventActivity.class);
				startActivity(i);

			}
		});

		
	}
	

	
	
	
	
	/**
	 * Custom class created purely to allow for clickable events
	 * 
	 * 
	 * @author Sean Wheeler
	 *
	 */
	
	
	public class MyOnItemSelectedListener implements OnItemSelectedListener {
		
		/**
		 * Darkens/Lightens and Completes/Incompletes an event on click
		 * 
		 */
	    public void onItemSelected(AdapterView<?> parent,
	        View view, int pos, long id) {
	    	
	    	String filtername = parent.getItemAtPosition(pos).toString();
	      
	    
	    
	      if(filtername.equals("All"))
	    	{
	    	  	events = MasterControlProgram.getToDoListEvents();
	    	  	adapter = new EventArrayAdapter<Event>(selfRef, R.layout.list_item, events);
	    	  	lv.setAdapter(adapter);
	    	  	Toast.makeText(parent.getContext(), "The filter is: " +
	    		          filtername, Toast.LENGTH_LONG).show();
	    	}
	      else if (filtername.equals("Complete"))
	    	{
	    		events = MasterControlProgram.filterEventsComplete();
	    		adapter = new EventArrayAdapter<Event>(selfRef, R.layout.list_item, events);
	    	  	lv.setAdapter(adapter);
	    	  	Toast.makeText(parent.getContext(), "The filter is: " +
	    		          filtername, Toast.LENGTH_LONG).show();
	    	}
	    	else if (filtername.equals("Incomplete"))
	    	{
	    		events = MasterControlProgram.filterEventsIncomplete();
	    		adapter = new EventArrayAdapter<Event>(selfRef, R.layout.list_item, events);
	    		lv.setAdapter(adapter);
	    		Toast.makeText(parent.getContext(), "The filter is: " +
	    		          filtername, Toast.LENGTH_LONG).show();
	    	}
	    	else if (filtername.equals("Date (After)"))
	    	{
	    		searchType = 2; //Set the search param
	    		Toast.makeText(parent.getContext(), "Search Bar set to: " +
	    		          filtername, Toast.LENGTH_LONG).show();
	    	}
	    	else if (filtername.equals("Date (Up To)"))
	    	{
	    		searchType = 1; //Set the search param
	    		Toast.makeText(parent.getContext(), "Search Bar set to: " +
	    		          filtername, Toast.LENGTH_LONG).show();
	    	}
	    	else if (filtername.equals("Type"))
	    	{
	    		searchType = 0; //Set the search param
	    		Toast.makeText(parent.getContext(), "Search Bar set to: " +
	    		          filtername, Toast.LENGTH_LONG).show();
	    		
	    	}
	    }

	    public void onNothingSelected(AdapterView parent) {
	      // Do nothing.
	    }
	}
	/**
	 * This is a static method that is a list of possible countries to use as
	 * locations
	 */
	/**
	static final String[] COUNTRIES = new String[] { "Afghanistan", "Albania",
			"Algeria", "American Samoa", "Andorra", "Angola", "Anguilla",
			"Antarctica", "Antigua and Barbuda", "Argentina", "Armenia",
			"Aruba", "Australia", "Austria", "Azerbaijan", "Bahrain",
			"Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin",
			"Bermuda", "Bhutan", "Bolivia", "Bosnia and Herzegovina",
			"Botswana", "Bouvet Island", "Brazil",
			"British Indian Ocean Territory", "British Virgin Islands",
			"Brunei", "Bulgaria", "Burkina Faso", "Burundi", "Cote d'Ivoire",
			"Cambodia", "Cameroon", "Canada", "Cape Verde", "Cayman Islands",
			"Central African Republic", "Chad", "Chile", "China",
			"Christmas Island", "Cocos (Keeling) Islands", "Colombia",
			"Comoros", "Congo", "Cook Islands", "Costa Rica", "Croatia",
			"Cuba", "Cyprus", "Czech Republic",
			"Democratic Republic of the Congo", "Denmark", "Djibouti",
			"Dominica", "Dominican Republic", "East Timor", "Ecuador", "Egypt",
			"El Salvador", "Equatorial Guinea", "Eritrea", "Estonia",
			"Ethiopia", "Faeroe Islands", "Falkland Islands", "Fiji",
			"Finland", "Former Yugoslav Republic of Macedonia", "France",
			"French Guiana", "French Polynesia", "French Southern Territories",
			"Gabon", "Georgia", "Germany", "Ghana", "Gibraltar", "Greece",
			"Greenland", "Grenada", "Guadeloupe", "Guam", "Guatemala",
			"Guinea", "Guinea-Bissau", "Guyana", "Haiti",
			"Heard Island and McDonald Islands", "Honduras", "Hong Kong",
			"Hungary", "Iceland", "India", "Indonesia", "Iran", "Iraq",
			"Ireland", "Israel", "Italy", "Jamaica", "Japan", "Jordan",
			"Kazakhstan", "Kenya", "Kiribati", "Kuwait", "Kyrgyzstan", "Laos",
			"Latvia", "Lebanon", "Lesotho", "Liberia", "Libya",
			"Liechtenstein", "Lithuania", "Luxembourg", "Macau", "Madagascar",
			"Malawi", "Malaysia", "Maldives", "Mali", "Malta",
			"Marshall Islands", "Martinique", "Mauritania", "Mauritius",
			"Mayotte", "Mexico", "Micronesia", "Moldova", "Monaco", "Mongolia",
			"Montserrat", "Morocco", "Mozambique", "Myanmar", "Namibia",
			"Nauru", "Nepal", "Netherlands", "Netherlands Antilles",
			"New Caledonia", "New Zealand", "Nicaragua", "Niger", "Nigeria",
			"Niue", "Norfolk Island", "North Korea", "Northern Marianas",
			"Norway", "Oman", "Pakistan", "Palau", "Panama",
			"Papua New Guinea", "Paraguay", "Peru", "Philippines",
			"Pitcairn Islands", "Poland", "Portugal", "Puerto Rico", "Qatar",
			"Reunion", "Romania", "Russia", "Rwanda", "Sqo Tome and Principe",
			"Saint Helena", "Saint Kitts and Nevis", "Saint Lucia",
			"Saint Pierre and Miquelon", "Saint Vincent and the Grenadines",
			"Samoa", "San Marino", "Saudi Arabia", "Senegal", "Seychelles",
			"Sierra Leone", "Singapore", "Slovakia", "Slovenia",
			"Solomon Islands", "Somalia", "South Africa",
			"South Georgia and the South Sandwich Islands", "South Korea",
			"Spain", "Sri Lanka", "Sudan", "Suriname",
			"Svalbard and Jan Mayen", "Swaziland", "Sweden", "Switzerland",
			"Syria", "Taiwan", "Tajikistan", "Tanzania", "Thailand",
			"The Bahamas", "The Gambia", "Togo", "Tokelau", "Tonga",
			"Trinidad and Tobago", "Tunisia", "Turkey", "Turkmenistan",
			"Turks and Caicos Islands", "Tuvalu", "Virgin Islands", "Uganda",
			"Ukraine", "United Arab Emirates", "United Kingdom",
			"United States", "United States Minor Outlying Islands", "Uruguay",
			"Uzbekistan", "Vanuatu", "Vatican City", "Venezuela", "Vietnam",
			"Wallis and Futuna", "Western Sahara", "Yemen", "Yugoslavia",
			"Zambia", "Zimbabwe" };
			*/

}